<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>fieldAverage 使用说明 | Giskard&#39;s CFD Learning Tricks</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="fieldAverage是 OpenFOAM 中的一种 functionObject，用来计算时均值。其基本用法是作为一个 function object 放在 controlDict 文件中，运行 solver 的同时计算指定场的时均值，以下是一个示例：
123456789101112131415161718192021222324functions&amp;#123;    fieldAverage1">
<meta property="og:type" content="article">
<meta property="og:title" content="fieldAverage 使用说明">
<meta property="og:url" content="http://xiaopingqiu.github.io/2015/04/12/fieldAverage/index.html">
<meta property="og:site_name" content="Giskard's CFD Learning Tricks">
<meta property="og:description" content="fieldAverage是 OpenFOAM 中的一种 functionObject，用来计算时均值。其基本用法是作为一个 function object 放在 controlDict 文件中，运行 solver 的同时计算指定场的时均值，以下是一个示例：
123456789101112131415161718192021222324functions&amp;#123;    fieldAverage1">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="fieldAverage 使用说明">
<meta name="twitter:description" content="fieldAverage是 OpenFOAM 中的一种 functionObject，用来计算时均值。其基本用法是作为一个 function object 放在 controlDict 文件中，运行 solver 的同时计算指定场的时均值，以下是一个示例：
123456789101112131415161718192021222324functions&amp;#123;    fieldAverage1">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

##ga('create', '[object Object]', 'auto');
ga('create', 'UA-62501686-1', 'auto');
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->


</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Giskard&#39;s CFD Learning Tricks</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">CFD and Scientific Computing</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
          <a class="main-nav-link" href="/atom.xml">Rss</a>
        
          <a class="main-nav-link" href="/about">About</a>
        
      </nav>
      <nav id="sub-nav">
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="q" value="site:http://xiaopingqiu.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-fieldAverage" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/04/12/fieldAverage/" class="article-date">
  <time datetime="2015-04-12T09:00:54.000Z" itemprop="datePublished">2015-04-12</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/OpenFOAM/">OpenFOAM</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      fieldAverage 使用说明
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><code>fieldAverage</code>是 OpenFOAM 中的一种 functionObject，用来计算时均值。其基本用法是作为一个 function object 放在 controlDict 文件中，运行 solver 的同时计算指定场的时均值，以下是一个示例：</p>
<figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">functions</span><br><span class="line">&#123;</span><br><span class="line">    fieldAverage1</span><br><span class="line">    &#123;</span><br><span class="line">        type            fieldAverage;</span><br><span class="line">        functionObjectLibs ( <span class="string">"libfieldFunctionObjects.so"</span> );</span><br><span class="line">        outputControl   outputTime;</span><br><span class="line">        fields</span><br><span class="line">        (</span><br><span class="line">            Ua</span><br><span class="line">            &#123;</span><br><span class="line">                 mean        <span class="function_start"><span class="keyword">on</span></span>;</span><br><span class="line">                 prime2Mean  off;</span><br><span class="line">                 base        <span class="property">time</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            Ub</span><br><span class="line">            &#123;</span><br><span class="line">                 mean        <span class="function_start"><span class="keyword">on</span></span>;</span><br><span class="line">                 prime2Mean  off;</span><br><span class="line">                 base        <span class="property">time</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>但是，在有效地使用<code>fieldAverage</code>之前，有一些问题需要澄清，其中最重要的一个是：<strong><code>fieldAverage</code>到底在对哪个时间段求的时间平均</strong>？本文通过对<code>fieldAverage</code>的源码进行分析，试图厘清这些细节，并给出一个可靠的<code>fieldAverage</code>使用说明。涉及到的源文件包括：<code>fieldAverage.H</code>, <code>fieldAverage.C</code>, <code>fieldAverageTemplates.C</code>, <code>fieldAverageItem.C</code>，位于目录<code>$WM_PROJECT_DIR/src/postProcessing/functionObjects/field/fieldAverage</code>下。</p>
<h5 id="首要问题：fieldAverage对哪个时间区间进行时均计算？">首要问题：<strong><code>fieldAverage</code>对哪个时间区间进行时均计算</strong>？</h5><p>上面这段代码是从我运行的一个算例的 controlDict 中摘出来的。算例运行过程中发现，每一个时间步都要调用计算时均值相关的代码，并屏幕上的输出也会打印出<code>Calculating averages</code>，而每一个输出数据文件里，都有<code>*Mean</code>数据，其中<code>*</code>表示在<code>controlDict</code>中定义了需要求解时均值的某个场。<br>经过摸索发现，跟时均值求解密切相关的一段代码位于<a href="http://foam.sourceforge.net/docs/cpp/a08780_source.html" target="_blank" rel="external"><code>fieldAverageTemplates.C</code></a>，下面是从中摘出来的一段核心代码：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">182</span> <span class="keyword">template</span>&lt;<span class="keyword">class</span> Type&gt;</span><br><span class="line"><span class="number">183</span> <span class="keyword">void</span> Foam::fieldAverage::calculateMeanFieldType(<span class="keyword">const</span> label fieldI) <span class="keyword">const</span></span><br><span class="line"><span class="number">184</span> &#123;</span><br><span class="line"><span class="number">185</span>  <span class="keyword">const</span> word&amp; fieldName = faItems_[fieldI].fieldName();</span><br><span class="line"><span class="number">187</span>  <span class="keyword">if</span> (obr_.foundObject&lt;Type&gt;(fieldName))</span><br><span class="line"><span class="number">188</span>  &#123;</span><br><span class="line"><span class="number">189</span>    <span class="keyword">const</span> Type&amp; baseField = obr_.lookupObject&lt;Type&gt;(fieldName);</span><br><span class="line"><span class="number">190</span> </span><br><span class="line"><span class="number">191</span>    Type&amp; meanField = <span class="keyword">const_cast</span>&lt;Type&amp;&gt;</span><br><span class="line"><span class="number">192</span>    (</span><br><span class="line"><span class="number">193</span>    obr_.lookupObject&lt;Type&gt;(faItems_[fieldI].meanFieldName())</span><br><span class="line"><span class="number">194</span>    );</span><br><span class="line"><span class="number">195</span> </span><br><span class="line"><span class="number">196</span>    scalar dt = obr_.time().deltaTValue();</span><br><span class="line"><span class="number">197</span>    scalar Dt = totalTime_[fieldI];</span><br><span class="line"><span class="number">198</span> </span><br><span class="line"><span class="number">199</span>    <span class="keyword">if</span> (faItems_[fieldI].iterBase())</span><br><span class="line"><span class="number">200</span>    &#123;</span><br><span class="line"><span class="number">201</span>      dt = <span class="number">1.0</span>;</span><br><span class="line"><span class="number">202</span>      Dt = scalar(totalIter_[fieldI]);</span><br><span class="line"><span class="number">203</span>    &#125;</span><br><span class="line"><span class="number">204</span> </span><br><span class="line"><span class="number">205</span>    scalar alpha = (Dt - dt)/Dt;</span><br><span class="line"><span class="number">206</span>    scalar beta = dt/Dt;</span><br><span class="line"><span class="number">207</span> </span><br><span class="line"><span class="number">208</span>    <span class="keyword">if</span> (faItems_[fieldI].window() &gt; <span class="number">0</span>)</span><br><span class="line"><span class="number">209</span>    &#123;</span><br><span class="line"><span class="number">210</span>      <span class="keyword">const</span> scalar w = faItems_[fieldI].window();</span><br><span class="line"><span class="number">211</span> </span><br><span class="line"><span class="number">212</span>      <span class="keyword">if</span> (Dt - dt &gt;= w)</span><br><span class="line"><span class="number">213</span>      &#123;</span><br><span class="line"><span class="number">214</span>        alpha = (w - dt)/w;</span><br><span class="line"><span class="number">215</span>        beta = dt/w;</span><br><span class="line"><span class="number">216</span>      &#125;</span><br><span class="line"><span class="number">217</span>    &#125;</span><br><span class="line"><span class="number">219</span>    meanField = alpha*meanField + beta*baseField;</span><br><span class="line"><span class="number">220</span>  &#125;</span><br><span class="line"><span class="number">221</span> &#125;</span><br></pre></td></tr></table></figure></p>
<p>以下是我根据理解整理的代码解析：</p>
<ol>
<li>变量<code>baseField</code>定义为当前时间步计算得到的场的值(Ua)，<code>meanField</code>定义为上一个时间步对应的场的时均值(UaMean)。</li>
<li><code>dt</code>定义为时间步长，<code>Dt</code>定义为当前所在的时间。注意199行，<code>iterBase()</code>这个函数定义为当<code>base</code> (见controlDict) 为<code>ITER</code>时，返回<code>true</code>。此时，<code>dt</code>定义为1，意义很显然，当以时间步数为基准的时候，下一步和上一步当然是只相差1个时间步；<code>Dt</code>则被定义为当前所在的时间步(第xx步)。所以，<code>Dt</code>和<code>dt</code>的真实含义取决于<code>base</code>。<strong><code>fieldAverageItem.C</code>中给<code>base</code>赋了默认值为<code>ITER</code></strong>。<code>base</code>可以指定为<code>time</code>，具体见下文。</li>
<li><p>alpha 定义为(Dt-dt)/Dt, beta 定义为 dt/Dt。然后当前时间步的<code>meanField</code>定义为<code>alpha*meanField + beta*baseField</code>。举例说明，假设dt = 1, Dt = 20, 假设上一时间步的时均值为<code>meanField_pre</code>，则新时刻时均值为</p>
<figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">19/20<span class="keyword">*</span>meanField_pre + 1/20<span class="keyword">*</span>baseField) = (19<span class="keyword">*</span>meanField_pre + baseField)/20</span><br></pre></td></tr></table></figure>
<p><code>19*meanField_pre</code>的含义很明显，前19步的时均值乘以总时间步数，也就是前 19 步 Field 值的加和，再加上当前时间步的值<code>baseField</code>，就是前 20 步的 Field 值的和。用这个值除以20，得到新时刻的时均值<code>meanField</code>。由此可见，在这种情况下，<strong>每个时间步输出的时均值是从开始时刻到当前时间步的Field值对总时间的平均</strong>。</p>
</li>
<li><p>很多时候，我们并不想得到从开始到结束整个模拟时段内的平均值，而希望得到指定时段内的平均值，这时，可以通过指定<code>window</code>变量的值来达到目的。代码208行，注意到这里出现了一个新函数<code>window</code>。可以查到<code>window</code>函数返回值默认是<code>-1</code>。但可以在controlDict中<code>base</code>下面定义变量<code>windows</code>的值，当<code>window</code>函数返回值大于0时，208-217将被执行。接着上一点的假设，dt = 1, 定义并初始化为<code>window</code>函数的返回值，假设为20。则： </p>
<ul>
<li>当 <code>Dt</code> 小于或等于20时，不满足<code>Dt - dt &gt;= w</code>，此时时均值将按照上一点叙述的进行计算。</li>
<li>当 <code>Dt</code>大于20以后，假设<code>Dt = 21</code>，此时 <code>alpha = (w-dt)/w = (20-1)/20</code>，<code>bata = dt/w = 1/19</code>。令<code>meanField_pre</code>为<code>Dt = 20</code> 时的时均值，则<code>Dt = 21</code>时的时均值<figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">meanField = 19/20 <span class="keyword">*</span> meanField_pre + 1/20 <span class="keyword">*</span> baseField = (19 <span class="keyword">*</span> meanField_pre + baseField)/20</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>对比发现，如果<code>window = -1</code>(默认值)，则<code>Dt = 21</code>的时均值应该是<code>20/21 * meanField_pre + 1/21 * baseField</code>。而定义了<code>windows = 20</code>以后，此处的<code>19 * meanField_pre</code> 可以理解为<code>Dt = 21</code>这一步之前的19步的Field值的和，加上<code>baseField</code>，则为<code>Dt = 21</code> 以及其之前19步，共20步的Field值的和。再除以20，则为 <code>Dt = 21</code>以及其之前19步这20步的时均值。<strong>所以，当设定<code>window</code>为一正整数<code>w</code>时，输出的时均值是当前时间步以及其之前<code>w-1</code>步，这<code>w</code>步内Field的时均值</strong>。</p>
</li>
</ol>
<p>附上一个更一般化的示例：<br><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">fieldAverage1</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">type</span> fieldAverage;</span><br><span class="line">    functionObjectLibs (<span class="string">"libfieldFunctionObjects.so"</span>);</span><br><span class="line">    resetOnRestart true;</span><br><span class="line">    resetOnOutput false;</span><br><span class="line">    startTime     0.1; <span class="comment">// 开始计算时均值的时间</span></span><br><span class="line">    endTime       1.0; <span class="comment">// 停止计算时均值的时间</span></span><br><span class="line">    outputControl   outputTime;</span><br><span class="line">    fields</span><br><span class="line">    (</span><br><span class="line">        <span class="keyword">U</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">mean</span>            <span class="keyword">on</span>;</span><br><span class="line">            prime2Mean      <span class="keyword">on</span>;</span><br><span class="line">            base            time; <span class="comment">//以物理时间为基础来计算平均，而不是时间步数。</span></span><br><span class="line">            <span class="keyword">window</span>          10.0;</span><br><span class="line">            windowName      w1; <span class="comment">//optional</span></span><br><span class="line">        &#125;</span><br><span class="line">        p</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">mean</span>            <span class="keyword">on</span>;</span><br><span class="line">            prime2Mean      <span class="keyword">on</span>;</span><br><span class="line">            base            time;</span><br><span class="line">        &#125;</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><code>resetOnRestart</code>的值决定当solver继续运行时，是否要读取最近一个时间步的<code>meanField</code>的值来计算接下来时刻的时均值；<code>resetOnOutput</code>，顾名思义，是否要在每一次输出到文件以后重置<code>meanField</code>的值。这两个开关的默认值都是<code>false</code>。<br><code>mean</code>这个开关的含义无需多言，计算公式如下：<br>$$ \overline {x} = \frac{1}{N} \sum \limits _{i=1}^{N} x_i $$<br><code>prime2Mean</code>的计算公式如下：<br>$$ \overline{x’ ^{\,2}} = \frac{1}{N}\displaystyle\sum\limits_{i=1}^N (x_i - \overline{x})^2 $$<br>所以，如果<code>prime2Mean</code>为<code>on</code>，<code>mean</code>必须为<code>on</code>。</p>
<p>此外，如果计算已经结束，controlDict 中定义的 function 仍可以用<code>execFlowFunctionObjects</code>来执行。只是，这样的运行只能利用输出到文件的数据来进行计算了。举例说，假如时间步长是 0.001s， 每 0.1s 输出一次，那么同样是1-2s的时均值，solver运行过程中求解的公式是:<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">( field.<span class="function"><span class="title">at</span><span class="params">(<span class="number">1.001</span>)</span></span> +field.<span class="function"><span class="title">at</span><span class="params">(<span class="number">1.002</span>)</span></span> + ... + field.<span class="function"><span class="title">at</span><span class="params">(<span class="number">2.0</span>)</span></span> )/<span class="number">1000</span></span><br></pre></td></tr></table></figure></p>
<p>而solver运行完以后利用<code>execFlowFunctionObjects</code>计算的时均值应该是：<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">( field.<span class="function"><span class="title">at</span><span class="params">(<span class="number">1.1</span>)</span></span> + field.<span class="function"><span class="title">at</span><span class="params">(<span class="number">1.2</span>)</span></span> + ... + field.<span class="function"><span class="title">at</span><span class="params">(<span class="number">2</span>)</span></span> )/<span class="number">10</span></span><br></pre></td></tr></table></figure></p>
<p>注意，这个结果没有经过直接的验证，是我根据原理推演的结果。<br>有时候，运行<code>execFlowFunctionObjects</code>会报错说找不到<code>phi</code>，这时加上<code>-noFlow</code>选项，就不会报错了。</p>
<h5 id="小结">小结</h5><ol>
<li><code>base</code>用来指定作时间平均的基础，是基于时间步数(ITER)还是物理时间(time);</li>
<li><code>window</code>用来作平均的时间段的长度，如果不设定，则求的是从开始到当前时间这个时间段的平均值。<code>window</code>的数值的实际含义依<code>base</code>而定，如果<code>base</code>是<code>ITER</code>，则<code>window=20</code>表示当前步及其前 19 个时间步从 20 个时间步内的平均，而如果<code>base</code>是<code>time</code>,则表示的是 20s 内的平均。</li>
</ol>
<p><strong>错误更正</strong>：<br>2015.09.05: 在 controlDict 里， <code>base</code> 只能是  <code>time</code> 或 <code>iteration</code>，而不能是上文中说的 <code>ITER</code>， <code>ITER</code> 只是代码中使用的一个代称 。感谢“启之” 指出！  </p>
<p><strong>补充</strong>:<br>2015.11.26：上文没有提 <code>prime2Mean</code> 的计算方法，今天仔细看了一下，记录如下。<br> <code>prime2Mean</code> 的公式上面提到了，计算代码需要结合三个个函数来看，分别是 fieldAverage.C 文件里的 <code>calcAverages</code> 函数（事实上，上文提到的计算时均值的函数 <code>calculateMeanFieldType</code> 也是在这个函数中调用的）以及 fieldAverageTemplates.C 中定义的 <code>calculatePrime2MeanFields</code> 和 <code>addMeanSqrToPrime2Mean</code> 两个函数。我们看 <code>calcAverages</code> 看起</p>
<figure class="highlight sml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">void <span class="type">Foam</span>::fieldAverage::calcAverages<span class="literal">()</span></span><br><span class="line">&#123;</span><br><span class="line">   ......</span><br><span class="line">   ......</span><br><span class="line">    <span class="type">Info</span>&lt;&lt; <span class="string">"    Calculating averages"</span> &lt;&lt; nl;</span><br><span class="line"></span><br><span class="line">    addMeanSqrToPrime2Mean&lt;scalar, scalar&gt;<span class="literal">()</span>;</span><br><span class="line">    addMeanSqrToPrime2Mean&lt;<span class="built_in">vector</span>, symmTensor&gt;<span class="literal">()</span>;</span><br><span class="line"></span><br><span class="line">    calculateMeanFields&lt;scalar&gt;<span class="literal">()</span>;</span><br><span class="line">    calculateMeanFields&lt;<span class="built_in">vector</span>&gt;<span class="literal">()</span>;</span><br><span class="line">    calculateMeanFields&lt;sphericalTensor&gt;<span class="literal">()</span>;</span><br><span class="line">    calculateMeanFields&lt;symmTensor&gt;<span class="literal">()</span>;</span><br><span class="line">    calculateMeanFields&lt;tensor&gt;<span class="literal">()</span>;</span><br><span class="line"></span><br><span class="line">    calculatePrime2MeanFields&lt;scalar, scalar&gt;<span class="literal">()</span>;</span><br><span class="line">    calculatePrime2MeanFields&lt;<span class="built_in">vector</span>, symmTensor&gt;<span class="literal">()</span>;</span><br><span class="line">  ......</span><br><span class="line">  ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以看到，计算时均值 <code>mean</code> ，其实只要调用 <code>calculateMeanFields</code> 函数就可以了，但是计算 <code>prime2Mean</code> 则分成两部分：分别是在计算 <code>mean</code> 之前调用 <code>addMeanSqrToPrime2Mean</code> ，以及在计算 <code>mean</code> 之后调用 <code>calculatePrime2MeanFields</code>。下面一一来看这两个函数，注意到 <code>addMeanSqrToPrime2Mean</code> 和 <code>calculatePrime2MeanFields</code> 两个函数其实分别是通过调用 <code>addMeanSqrToPrime2MeanType</code> 和 <code>calculatePrime2MeanFieldType</code> 来其作用的，所以这里只看真正执行计算任务的<code>addMeanSqrToPrime2MeanType</code> 和 <code>calculatePrime2MeanFieldType</code> 函数。</p>
<ul>
<li>addMeanSqrToPrime2MeanType<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">class</span> Type1, <span class="keyword">class</span> Type2&gt;</span><br><span class="line"><span class="keyword">void</span> Foam::fieldAverage::addMeanSqrToPrime2MeanType(<span class="keyword">const</span> label fieldI) <span class="keyword">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">const</span> word&amp; fieldName = faItems_[fieldI].fieldName();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (obr_.foundObject&lt;Type1&gt;(fieldName))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">const</span> Type1&amp; meanField =</span><br><span class="line">            obr_.lookupObject&lt;Type1&gt;(faItems_[fieldI].meanFieldName());</span><br><span class="line"></span><br><span class="line">        Type2&amp; prime2MeanField = <span class="keyword">const_cast</span>&lt;Type2&amp;&gt;</span><br><span class="line">        (</span><br><span class="line">            obr_.lookupObject&lt;Type2&gt;(faItems_[fieldI].prime2MeanFieldName())</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        prime2MeanField += sqr(meanField);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>这个函数很简单，就是直接往上一步的 <code>prime2MeanField</code> 加上上一步的 <code>meanField</code> 。</p>
<ul>
<li>calculatePrime2MeanFieldType<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">class</span> Type1, <span class="keyword">class</span> Type2&gt;</span><br><span class="line"><span class="keyword">void</span> Foam::fieldAverage::calculatePrime2MeanFieldType(<span class="keyword">const</span> label fieldI) <span class="keyword">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">const</span> word&amp; fieldName = faItems_[fieldI].fieldName();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (obr_.foundObject&lt;Type1&gt;(fieldName))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">const</span> Type1&amp; baseField = obr_.lookupObject&lt;Type1&gt;(fieldName);</span><br><span class="line">        <span class="keyword">const</span> Type1&amp; meanField =</span><br><span class="line">            obr_.lookupObject&lt;Type1&gt;(faItems_[fieldI].meanFieldName());</span><br><span class="line"></span><br><span class="line">        Type2&amp; prime2MeanField = <span class="keyword">const_cast</span>&lt;Type2&amp;&gt;</span><br><span class="line">        (</span><br><span class="line">            obr_.lookupObject&lt;Type2&gt;(faItems_[fieldI].prime2MeanFieldName())</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        scalar dt = obr_.time().deltaTValue();</span><br><span class="line">        scalar Dt = totalTime_[fieldI];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (faItems_[fieldI].iterBase())</span><br><span class="line">        &#123;</span><br><span class="line">            dt = <span class="number">1.0</span>;</span><br><span class="line">            Dt = scalar(totalIter_[fieldI]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        scalar alpha = (Dt - dt)/Dt;</span><br><span class="line">        scalar beta = dt/Dt;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (faItems_[fieldI].window() &gt; <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">const</span> scalar w = faItems_[fieldI].window();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (Dt - dt &gt;= w)</span><br><span class="line">            &#123;</span><br><span class="line">                alpha = (w - dt)/w;</span><br><span class="line">                beta = dt/w;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        prime2MeanField =</span><br><span class="line">            alpha*prime2MeanField</span><br><span class="line">          + beta*sqr(baseField)</span><br><span class="line">          - sqr(meanField);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>这个函数里， <code>alpha</code> 与 <code>beta</code> 的定义跟上文计算时均值的函数 <code>calculateMeanFieldType</code> 是一样的，关键在于最后的那一句。<br>那么，这两个函数结合起来，如何就等价于上文给出的 <code>prime2MeanField</code> 的计算公式了呢？下面做一个简单的分析：<br>t = 1 时，某个场 $x$ 记为 $x_1$ ， <code>meanField</code> 记为 $m_1$，等于 $x_1$ ， <code>prime2MeanField</code> 记为 $pM_1$，等于 0；</p>
<p>t = 2 时，此刻的 $x$ 记为 $x_2$ ， <code>meanField</code> 记为 $m_2$，等于 $(x_1+x_2)/2$， <code>prime2MeanField</code> 的理论值等于 $\frac{1}{2} [(x_1-m_2)^2+(x_2-m_2)^2]$，而按照代码，此刻的 <code>prime2MeanField</code> （记为$pM_2$）的计算分两步， 先是计算当前步的 <code>meanField</code> 之前，执行 </p>
<p>$$<br>prime2MeanField = pM_1 + m_1^2<br>$$</p>
<p>然后是在计算当前步的 <code>meanField</code> 之后，执行</p>
<p>$$<br>prime2MeanField = \frac{1}{2}\cdot(prime2MeanField) + \frac{1}{2}\cdot x_2^2 - m_2^2<br>$$</p>
<p>结合起来，得到 </p>
<p>$$<br>\begin{align}<br>pM_2 = &amp; \frac{1}{2}\cdot (pM_1+m_1^2) + \frac{1}{2}\cdot x_2^2 - m_2^2  \\<br>= &amp; \frac{1}{2}\cdot m_1^2 + \frac{1}{2}\cdot x_2^2 - m_2^2  \\<br>= &amp; \frac{1}{2}\cdot x_1^2 + \frac{1}{2}\cdot x_2^2 - m_2^2<br>\end{align}<br>$$</p>
<p>由于 $2\cdot m_2 = x_1 + x_2$ ，于是，上式可以进一步化简， </p>
<p>$$<br>\begin{align}<br>pM_2 = &amp; \frac{1}{2}\cdot x_1^2 + \frac{1}{2}\cdot x_2^2 - 2\cdot m_2^2 + m_2^2  \\<br>= &amp; \frac{1}{2}\cdot x_1^2 + \frac{1}{2}\cdot x_2^2 -(x_1+x_2)\cdot m_2 + m_2^2  \\<br>= &amp; \frac{1}{2}\cdot (x_1^2 -2\cdot x_1^2 + m_2^2) + \frac{1}{2}\cdot (x_2^2 -2\cdot x_2^2 + m_2^2) = \frac{1}{2}\cdot (x_1-m_2)^2 + \frac{1}{2}\cdot (x_2-m_2)^2<br>\end{align}<br>$$</p>
<p>这就与理论值刚好对上了；</p>
<p>t = 3时，也可以类推下去，这里简略写一下：<br>$$<br>\begin{align}<br>pM_3 = &amp; \frac{2}{3}\cdot (pM_2 + m_2^2) + \frac{1}{3}\cdot x_3^2 - m_3^2 \\<br>= &amp; \frac{1}{3}\cdot (x_1^2 + x_2^2 + x_3^2) - m_3^2  \\<br>= &amp; \frac{1}{3}\cdot (x_1^2 + x_2^2 + x_3^2) - \frac{2}{3}\cdot (x_1+x_2+x_3)\cdot m_3 + m_3^2 \\<br>= &amp; \frac{1}{3}\cdot [(x_1-m_3)^2 + (x_2-m_3)^2 + (x_3-m_3)^2]<br>\end{align}<br>$$</p>
<p>同样是跟理论值是一样的。 </p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://xiaopingqiu.github.io/2015/04/12/fieldAverage/" data-id="cj2lay346003lkkmbp14866fw" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/OpenFOAM/">OpenFOAM</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Postprocessing/">Postprocessing</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/05/03/paraview-remote/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          ParaView 的远程模式
        
      </div>
    </a>
  
  
    <a href="/2015/04/04/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <!-- 多说评论框 start -->
  <div class="ds-thread" data-thread-key="post-fieldAverage" data-title="fieldAverage 使用说明" data-url="http://xiaopingqiu.github.io/2015/04/12/fieldAverage/"></div>
  <!-- 多说评论框 end -->
  <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
  <script type="text/javascript">
  var duoshuoQuery = {short_name:"xiaopingqiu"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
  <!-- 多说公共JS代码 end -->
</section>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/C/">C++</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/DEM/">DEM</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/OpenFOAM/">OpenFOAM</a><span class="category-list-count">44</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Paraview/">Paraview</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/swak4Foam/">swak4Foam</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/test/">test</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/vim/">vim</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Boundary-conditions/">Boundary conditions</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C++</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CentOS/">CentOS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Code-Explained/">Code Explained</a><span class="tag-list-count">29</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/LES/">LES</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/LIGGGHTS/">LIGGGHTS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ODE/">ODE</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OpenFOAM/">OpenFOAM</a><span class="tag-list-count">20</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Postprocessing/">Postprocessing</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Preprocessing/">Preprocessing</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RTS/">RTS</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TIL/">TIL</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Windows/">Windows</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fvOptions/">fvOptions</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovyBC/">groovyBC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/paraview/">paraview</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/test/">test</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/thermophysicalModels/">thermophysicalModels</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/turbulence-model/">turbulence model</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vim/">vim</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/wall-functions/">wall functions</a><span class="tag-list-count">4</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Boundary-conditions/" style="font-size: 15.56px;">Boundary conditions</a><a href="/tags/C/" style="font-size: 11.11px;">C++</a><a href="/tags/CentOS/" style="font-size: 10px;">CentOS</a><a href="/tags/Code-Explained/" style="font-size: 20px;">Code Explained</a><a href="/tags/LES/" style="font-size: 10px;">LES</a><a href="/tags/LIGGGHTS/" style="font-size: 10px;">LIGGGHTS</a><a href="/tags/ODE/" style="font-size: 10px;">ODE</a><a href="/tags/OpenFOAM/" style="font-size: 18.89px;">OpenFOAM</a><a href="/tags/Postprocessing/" style="font-size: 17.78px;">Postprocessing</a><a href="/tags/Preprocessing/" style="font-size: 11.11px;">Preprocessing</a><a href="/tags/RTS/" style="font-size: 12.22px;">RTS</a><a href="/tags/TIL/" style="font-size: 10px;">TIL</a><a href="/tags/Windows/" style="font-size: 10px;">Windows</a><a href="/tags/fvOptions/" style="font-size: 11.11px;">fvOptions</a><a href="/tags/groovyBC/" style="font-size: 10px;">groovyBC</a><a href="/tags/paraview/" style="font-size: 10px;">paraview</a><a href="/tags/test/" style="font-size: 11.11px;">test</a><a href="/tags/thermophysicalModels/" style="font-size: 14.44px;">thermophysicalModels</a><a href="/tags/turbulence-model/" style="font-size: 16.67px;">turbulence model</a><a href="/tags/vim/" style="font-size: 10px;">vim</a><a href="/tags/wall-functions/" style="font-size: 13.33px;">wall functions</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/">五月 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">四月 2016</a><span class="archive-list-count">12</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">三月 2016</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">十二月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">十一月 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/10/">十月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/09/">九月 2015</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">八月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/06/">六月 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2017/05/12/duoshuoAnnouncement/">多说评论系统将停止提供服务</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewScritps/">Paraview 脚本一例</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewCamera/">Paraview 中有关 Camera 的操作两例</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewCustomFilter/">Paraview 中创建 Custom Filter</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewStreamLineOnSlice/">在 Paraview 中画截面上的流线</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 Giskard Q.<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

<script src="//dn-lbstatics.qbox.me/lbservice/busuanzi/2.0/busuanzi.mini.js"/></script>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/atom.xml" class="mobile-nav-link">Rss</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-62501686-1', 'auto');
  ga('send', 'pageview');

</script>

  </div>
<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</body>
</html>